<template>
	<el-container>
		<el-container>
            <el-header>
                <div>
                    <el-input v-model="search.keywords" placeholder="请输入关键词" clearable></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="add">查询</el-button>
                </div>
            </el-header>
            <el-header>
                <el-button type="primary" icon="el-icon-plus" @click="add">新增审批流程</el-button>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<el-table-column label="ID号" type="index"  align="center" width="80"></el-table-column>
					<el-table-column label="流程名称" prop="title" align="center"></el-table-column>
					<el-table-column label="流程类型" prop="check_type" align="center">
                        <template #default="scope">
                            <div v-if="scope.row.check_type==1">固定审批流</div>
                            <div v-if="scope.row.check_type==2">自由审批流</div>
                        </template>
                    </el-table-column>
					<el-table-column label="最后修改人" prop="last_user_name" align="center"></el-table-column>
                    <el-table-column label="状态" prop="system_disable" align="center">
                        <template #default="scope">
                            <el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1" inline-prompt :active-text="'启用'" :inactive-text="'禁用'"></el-switch>
						</template>
                    </el-table-column>
					<el-table-column label="操作" align="center" width="180">
						<template #default="scope">
							<div class="buttonFlex">
								<el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button type="danger" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</div>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
	</el-container>
</template>

<script>
import saveDialog from './save'
export default {
	name: 'user',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			groupFilterText: '',
			group: [],
			apiObj: this.$API.setting.approval.lists,
			selection: [],
			search: {
				keywords: null,
			},
			//所需数据选项
			cates: [],
            data:[
                {id:0,name:'ceshi'},
                {id:1,name:'ceshi'},
            ]
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        table_detail(row){
            this.$router.push({
			path: '/device/list/detail',
            query:{
                id:row.id
            }
		})
        },
		//编辑
		table_edit(row) {
            console.log('row :>> ', row);
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        goLocation(){
            this.$router.push({
                path: 'storeroomLocation',
            })
        },
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.setting.approval.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.upData(this.search)
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
            this.printArr = ''
		},
        selectionChange(selection) {
			this.selection = selection;
		},
        async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.setting.approval.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
.shopInfo{
    display: flex;
    align-items: center;
}
.img{
	width: 50px;
	height: 50px;
}
</style>
